package com.lanou3g.web;

import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanou3g.bean.Admin;
import com.lanou3g.bean.Menu;
import com.lanou3g.bean.Role;
import com.lanou3g.service.AdminService;
import com.lanou3g.service.RoleService;
import com.lanou3g.util.EncryptUtil;
import com.lanou3g.util.Result;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private AdminService adminService;
	@Autowired
	private RoleService roleService;
	
	//这个handler用于处理前端页面登录按钮的登录事件
	@ResponseBody
	@RequestMapping("/login")
	public Result login(String username, String password,HttpSession session) {
//		对浏览器传过来的密码进行md5加密.拿加密后的密码和数据库中的密码比对(因为数据库中的密码是md5之后的密码)
		password = EncryptUtil.encrypt(password);
		
		//根据用户名和密码查询用户
		Admin admin = adminService.findAdminByUsernameAndPassword(username, password);
		System.out.println(admin);
		if(admin == null) {
			//如果没有查询到这个用户. 提示用户名或者密码错误
			return new Result(1, "用户名或者密码错误");
		}else {
			//如果查询到了这个用户.将用户添加到session中,同时返回成功登录的状态
			session.setAttribute("admin", admin);
			return new Result(0, "登录成功");
		}
	}
	
	//跳转到主页面.即 jsp文件夹里的 allmain.jsp文件
	@RequestMapping("/allmain")
	public String allMain() {
		return "allmain";
	}
	
	//获取主页面左侧菜单栏信息
	@ResponseBody
	@RequestMapping("/getMenus")
	public List<Menu> getMenus(HttpSession session) {
		Admin admin = (Admin)session.getAttribute("admin");
		System.out.println("getMenus:"+admin);
		return adminService.getMenusByRoleId(admin.getRoleId());
	}
	
	//显示后台主页(正文区域)
	@RequestMapping("/main")
	public String systemMain() {
		return "main";
	}
	
	//个人信息页面
	@RequestMapping("/personal")
	public String personal(HttpSession session) {
		Admin admin = (Admin)session.getAttribute("admin");
		Integer id = admin.getId();
		admin = adminService.findAdminById(id);
		session.setAttribute("admin", admin);
		return "admin/personalInfo";
	}
	
	//退出系统
	@RequestMapping("/logOut")
	public String logout(HttpSession session) {
		Enumeration em = session.getAttributeNames();
		while(em.hasMoreElements()){
			String attribute = em.nextElement().toString();
			session.removeAttribute(attribute);
		}
		return "redirect:/index.jsp";
	}
	
	//更新用户信息
	@ResponseBody
	@RequestMapping("/updateAdmin")
	public Result updatePersonalInfo(Admin admin, HttpSession session) {
		System.out.println("需要更新的admin是:" + admin);
		int rows = adminService.updateAdmin(admin);
		System.out.println("成功更新"+ rows +"条数据");
		if(rows != 0) {
			return new Result(0,"修改成功");
		}else {
			return new Result(1,"修改失败,请重试");
		}
	}
	
	//跳转到修改密码界面
	@RequestMapping("/changePassword")
	public String changePassword(HttpSession session) {
		Admin admin = (Admin)session.getAttribute("admin");
		Integer id = admin.getId();
		admin = adminService.findAdminById(id);
		session.setAttribute("admin", admin);
		return "admin/changePassword";
	}
	
	//修改密码 
	@ResponseBody
	@RequestMapping("/changeAdminPassword")
	public Result changeAdminPassword(Integer id, String oldPassword, String newPassword1) {
		Admin admin = adminService.findAdminById(id);
		oldPassword = EncryptUtil.encrypt(oldPassword);
		if(admin.getPassword().equals(oldPassword)) {
			//如果旧密码是对的.才进行修改密码.
			Admin newAdmin = new Admin();
			newAdmin.setId(admin.getId());
			newAdmin.setPassword(EncryptUtil.encrypt(newPassword1));
			adminService.updateAdmin(newAdmin);
			return new Result(0,"修改成功");
		}else {
			return new Result(1,"原密码有误!");
		}
	}
	
	//进入管理员列表页面
	@RequestMapping("/adminList")
	public String adminList() {
		return "admin/adminList";
	}
	
	//获取管理员列表
	@ResponseBody
	@RequestMapping("/getAdminList")
	public Result getAdminList(Integer page, Integer limit) {
		System.out.println("page = " + page + ",limit = " + limit);
		return adminService.getAdminsByPage(page, limit);
	}
	
	//进入添加页面
	@RequestMapping("/addAdmin")
	public String addAdmin(HttpSession session) {
		//先获取所有的角色,存入session,再进入添加管理员页面.
		List<Role> roles = roleService.getAllRoles();
		session.setAttribute("roles", roles);
		return "admin/addAdmin";
	}
	
	@ResponseBody
	@RequestMapping("/checkAdminName/{username}")
	public Result checkAdminName(@PathVariable("username") String username) {
//		System.out.println(username);
		Admin admin = adminService.getAdminByUsername(username);
		if(admin == null) {
			//如果没有查到这个admin,说明这个用户还没有被注册
			return new Result(0,"用户没有被注册,可以添加");
		}else {
			return new Result(1,"用户已经存在,请换个别的名字");
		}
	}
	
	@ResponseBody
	@RequestMapping("/insAdmin")
	public Result insAdmin(Admin admin) {
		//密码加密
		admin.setPassword(EncryptUtil.encrypt(admin.getPassword()));
		Integer roleId = admin.getRoleId();
		Role role = roleService.getRoleById(roleId);
		admin.setRoleName(role.getRoleName());
		System.out.println(admin);
		int rows = adminService.insertAdmin(admin);
		
		if(rows != 0) {
			return new Result(0,"添加成功!");
		}else {
			return new Result(1,"添加失败!");
		}
	}
	
	//删除指定id的Admin
	@ResponseBody
	@RequestMapping("/delAdminById/{id}")
	public Result delAdminById(@PathVariable("id")Integer id) {
		int rows = adminService.delAdminById(id);
		if(rows == 0) {
			//如果删除掉的数据条数是0,表示没有删除
			return new Result(1,"删除失败!");
		}else {
			return new Result(0,"删除成功!");
		}
	}
	
	//进入编辑Admin页面
	@RequestMapping("/editAdmin/{id}")
	public String editAdmin(@PathVariable("id")Integer id,HttpSession session) {
		//找到要编辑的admin
		Admin admin = adminService.findAdminById(id);
		//存入session
		session.setAttribute("modifyAdmin", admin);
		//获取所有的角色
		List<Role> roles = roleService.getAllRoles();
		session.setAttribute("roles", roles);
		return "admin/editAdmin";
	}
	
	
	
}
